package com.bs.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.product.entity.Product;
import com.bs.product.entity.Type;
import com.bs.product.entity.dto.ProductDto;
import com.bs.product.service.IProductService;
import com.bs.product.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lukang
 * @since 2022-11-05
 */
@RestController
@RequestMapping("/product")
@Api("商品管理controller")
public class ProductController {
    @Autowired
    IProductService service;


    //根据id进行查询
    @ApiOperation("根据id进行查询")
    //@RequestMapping("get/{id}")
    @GetMapping("get/{id}")
    public AjaxResult getByID(@PathVariable("id") Long id){
        Product pro = service.getById(id);
        return AjaxResult.success(pro);

    }


    //分页查询
   // @ApiOperation("分页查询")
    //@GetMapping("/page")
    //@RequestMapping("/page")
    public IPage<Product> page(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,Product product){
        //构建page
        IPage<Product> page =new Page<>(pageNum,pageSize);
       //构建条件查询
        LambdaQueryWrapper<Product> wrapper =new LambdaQueryWrapper<>();
        wrapper.like(product.getName() !=null && product.getName().equals(""),Product::getName,product.getName())
                .eq(product.getTypeid() != null,Product::getTypeid,product.getTypeid());

        IPage<Product> productPage = service.page(page, wrapper);
    return productPage;
    }

    //查询所有
//    @RequestMapping("/list")
//    //public List<Product> lsit(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,Product product){
//    public List<Product> lsit(){
//        //构建page
//        //IPage<Product> page =new Page<>(pageNum,pageSize);
//        //构建条件查询
////        LambdaQueryWrapper<Product> wrapper =new LambdaQueryWrapper<>();
////        wrapper.like(product.getName() !=null && product.getName().equals(""),Product::getName,product.getName())
////                .eq(product.getTypeid() != null,Product::getTypeid,product.getTypeid());
//
////        List<Product> list = service.list();
////        return list;
//
//
//
//    }
    //单表查询所有 type 为空
    @ApiOperation("查询所有")
   // @RequestMapping("/getAll")
    @GetMapping("/getAll")
    public AjaxResult getAll(){
        List<Product> list = service.list();
        return AjaxResult.success(list);
    }
    //单表查询带条件
    @ApiOperation("单表带条件查询")
    @GetMapping("/seaechList")
   // @RequestMapping("/seaechList")
    public AjaxResult seaechList(ProductDto dto){
        //构建QuerWarrpper
        LambdaQueryWrapper<Product> lqw =new LambdaQueryWrapper();
        lqw.like(dto.getName() != null && !dto.getName().equals(""),
                Product::getName,dto.getName())
                .le(dto.getPrice() != null && dto.getPrice() != BigDecimal.ZERO
                        ,Product::getPrice,dto.getPrice())
                .ge(dto.getPrice1() != null && dto.getPrice1() != BigDecimal.valueOf(0)
                        ,Product::getPrice,dto.getPrice1())
                .eq(dto.getTypeid() != null ,Product::getTypeid,dto.getTypeid());

        List<Product> list = service.list(lqw);
        return AjaxResult.success(list);
    }
    //单表带条件分页查询
    //单表查询带条件
    @ApiOperation("单表带条件分页")
    @GetMapping("/seaechPage")
    //@RequestMapping("/seaechPage")
    public AjaxResult seaechPage(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum
                                 ,@RequestParam(value = "pageSize",defaultValue = "10")int pageSize,ProductDto dto){
        //构建分页对象
        IPage<Product> page = new Page<>(pageNum,pageSize);
        //构建QuerWarrpper
        LambdaQueryWrapper<Product> lqw =new LambdaQueryWrapper();
        lqw.like(dto.getName() != null && !dto.getName().equals(""),
                Product::getName,dto.getName())
                .le(dto.getPrice() != null && dto.getPrice() != BigDecimal.ZERO
                        ,Product::getPrice,dto.getPrice())
                .ge(dto.getPrice1() != null && dto.getPrice1() != BigDecimal.valueOf(0)
                        ,Product::getPrice,dto.getPrice1())
                .eq(dto.getTypeid() != null ,Product::getTypeid,dto.getTypeid());

        page= service.page(page, lqw);
        return AjaxResult.success(page);

    }
    @ApiOperation("联表查询所有")
    @GetMapping("/lbgeAll")
   // @RequestMapping("/lbgeAll")
    public  AjaxResult lbgeAll(){
        List<Product> products = service.lbgeAll();
        return AjaxResult.success(products);
    }
    //链表条件查询
    @ApiOperation("链表带条件查询")
    @GetMapping("/lbSearch")
    //@RequestMapping("/lbSearch")
    public  AjaxResult lbSearch(ProductDto dto){
        List<Product> products = service.lbSearch(dto);
        return AjaxResult.success(products);
    }
    //链表条件查询分页
    @ApiOperation("链表条件分页")
    @GetMapping("/lbpage/{pageNum}/{pageSize}")
    //@RequestMapping("/lbpage/{pageNum}/{pageSize}")
    public  AjaxResult lbpage(@PathVariable("pageNum") int pageNum,@PathVariable("pageSize") int pageSize, ProductDto dto){
       IPage<Product> page = new Page<>(pageNum,pageSize);
     page =  service.lbSearch(page,dto);
        return AjaxResult.success(page);
    }
    @ApiOperation("联表带条件分页")
    //@RequestMapping("/lbwp/{pageNum}/{pageSize}")
    @GetMapping("/lbwp/{pageNum}/{pageSize}")
    public  AjaxResult lbwp(@PathVariable("pageNum") int pageNum,@PathVariable("pageSize") int pageSize, ProductDto dto){
        IPage<Product> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Product> lqw =new LambdaQueryWrapper();
        lqw.like(dto.getName() != null && !dto.getName().equals(""),
                Product::getName,dto.getName())
                .le(dto.getPrice() != null && dto.getPrice() != BigDecimal.ZERO
                        ,Product::getPrice,dto.getPrice())
                .ge(dto.getPrice1() != null && dto.getPrice1() != BigDecimal.valueOf(0)
                        ,Product::getPrice,dto.getPrice1())
                .eq(dto.getTypeid() != null ,Product::getTypeid,dto.getTypeid());

        page =  service.qwpage(page,lqw);
        return AjaxResult.success(page);
    }

    //添加
    @ApiOperation("商品添加")
   // @RequestMapping("/add")
    @PostMapping
    public AjaxResult add(Product product){
        boolean save = service.save(product);
        if(save){
            return  AjaxResult.success("添加成功");
        }else{
            return  AjaxResult.fail("添加失败");
        }
    }

    //修改
    @ApiOperation("商品修改")
   // @RequestMapping("/update")
    @PutMapping
    public AjaxResult update(@RequestBody Product product){
        boolean update = service.updateById(product);
        if(update){
            return  AjaxResult.success("修改成功");
        }else{
            return  AjaxResult.fail("修改失败");
        }
    }

    //根据id进行删除
    @ApiOperation("商品删除")
    //@RequestMapping("/del/{id}")
    @DeleteMapping("/{id}")
    public AjaxResult del(@ApiParam("商品分类ID") @PathVariable("id") long id){
        boolean del = service.removeById(id);
        if(del){
            return  AjaxResult.success("删除成功");
        }else{
            return  AjaxResult.fail("删除失败");
        }

    }




}

